package com.neusoft.service.impl;

import com.neusoft.domain.Business;
import com.neusoft.domain.Cart;
import com.neusoft.domain.Food;
import com.neusoft.domain.vo.CartVo;
import com.neusoft.mapper.BusinessDao;
import com.neusoft.mapper.CartDao;
import com.neusoft.mapper.FoodDao;
import com.neusoft.service.CartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {
    // 创建CartDao对象
    @Autowired
    CartDao cartDao;
    // 创建FoodDao对象
    @Autowired
    FoodDao foodDao ;
    // 创建BusinessDao对象
    @Autowired
    BusinessDao businessDao;


    @Override
    public List<CartVo> listCart(Cart cart)  {
        List<Cart> cartList = cartDao.listCart(cart);

        List<CartVo> cartVoList = new ArrayList<>();
        for (Cart c : cartList) {
            CartVo cartVo = new CartVo();
            //对象拷贝
            BeanUtils.copyProperties(c,cartVo);

            Food food = foodDao.getFoodById(c.getFoodId());
            if (food != null) {
                cartVo.setFood(food);
            }
            Business business = businessDao.getBusinessById(cart.getBusinessId());
            if (business != null) {
                cartVo.setBusiness(business);
            }

            cartVoList.add(cartVo);
        }
        return cartVoList;
    }

    @Override
    public Integer saveCart(Cart cart)  {
        cart.setQuantity(1); // 默认数量为1
        int i = cartDao.saveCart(cart);
        return i;
    }

    @Override
    public Integer removeCart(Cart cart)  {
        int i = cartDao.removeCart(cart);
        return i;
    }

    @Override
    public Integer updateCart(Cart cart) {
        int i = cartDao.updateCart(cart);
        return i;
    }
}
